<template>
  <div>
    <Loading :loading="loading" />
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="Name" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="Age" prop="age">
        <el-input type="number" v-model="form.age" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSubmit">Submit</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Loading from './Loading.vue';
import request from '../utils/request';

export default {
  name: 'FormPage',
  components: {
    Loading
  },
  data() {
    return {
      form: {
        name: '',
        age: ''
      },
      rules: {
        name: [
          { required: true, message: 'Name is required', trigger: 'blur' }
        ],
        age: [
          { required: true, message: 'Age is required', trigger: 'blur' }
        ]
      },
      loading: false
    };
  },
  computed: {
    loading() {
      return this.$store.state.loading;
    }
  },
  methods: {
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          request.post('/submit', this.form)
              .then(() => {
                this.$message.success('Submitted successfully');
              });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
/* 样式代码 */
</style>